package vcp.server.deamontasks;

import java.util.List;
import java.util.TimerTask;

import org.joda.time.DateTime;

import vcp.entity.Lot;
import vcp.enums.LotStatus;
import vcp.enums.SiteEnum;
import vcp.server.DAO.LotDAO;
import vcp.server.interfaces.IServer;
import vcp.server.utility.SystemClock;

/**
 * 
 * @author VCP team
 *         <p>
 *         class that cleans up reservation at end of day
 *         </p>
 */
public class EndDayReservationCleanUpTask extends TimerTask
{
	private final IServer server;

	/**
	 * class constructor
	 * 
	 * @param serverDeamon
	 */
	public EndDayReservationCleanUpTask ( IServer serverDeamon )
	{
		this.server = serverDeamon;
	}

	// ****************************************************************************************************************************
	// TimerTask methods
	// ****************************************************************************************************************************
	@Override
	public void run ()
	{
		synchronized (this.server.getDBLock())
		{
			System.out.println("EndDayReservationCleanUpTask:  TrueTime:" + new DateTime() + "  SysTime:" + SystemClock.getClockInstance().getSystemDateTime());
			LotDAO lotDAO = new LotDAO();
			for ( SiteEnum site : SiteEnum.values() )
			{
				List <Lot> lastDayReserevedLots = lotDAO.findAllReserevedLots(site);
				for ( Lot lot : lastDayReserevedLots )
				{
					lotDAO.updateLotStatusByID(lot.getSiteID(), lot.getFloor(), lot.getRow(), lot.getColumn(), LotStatus.FREE);
					lotDAO.updateReservedBySubscription(lot.getSiteID(), lot.getFloor(), lot.getRow(), lot.getColumn(), null);
					lotDAO.updateCarInLot(lot.getSiteID(), lot.getFloor(), lot.getRow(), lot.getColumn(), null);
				}
			}
		}
	}

}
